package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyMedalAdapter;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MyMedalEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.AlertUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Create by LiaoDuanHong
 * Create on 2020/12/30 17:54
 */
public class MyMedalFragment extends Fragment {
    @BindView(R.id.rv_my_medal_list)
    RecyclerView rvMyMedalList;
    private MyMedalAdapter medalAdapter;
    private int type;

    public static Fragment newInstance(int key) {
        Fragment fragment = new MyMedalFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("point", key);
        fragment.setArguments(bundle);
        return fragment;
    }

    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragemnt_my_medal, container, false);
            ButterKnife.bind(this, view);
            type = getArguments().getInt("point");
            initView();
            initData();
        }
        return view;
    }

    private void initData() {
        String str = "?type=%s";
        AlertUtils.showProgressDialog(getActivity(), "", "加载中...", false);
        new UserAPI().getMyMedal(String.format(str, (type + 1)), new BaseResultCallback<MyMedalEntity>() {
            @Override
            public void onResponse(MyMedalEntity response) {
                AlertUtils.closeProgressDialog();
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        medalAdapter.setNewData(response.data);
                    }
                }
            }
        });
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvMyMedalList.setLayoutManager(layoutManager);
        medalAdapter = new MyMedalAdapter(getActivity(), type + 1);
        rvMyMedalList.setAdapter(medalAdapter);
        medalAdapter.setOnClickListener(new MyMedalAdapter.OnClickListener() {
            @Override
            public void ocClick(MyMedalEntity.DataBean item, int position) {
                medalAdapter.notifyItemChanged(position);
            }
        });
    }
}
